package com.hengxinguotong.zhihuichengjian.ui.quality;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/12/18.
 */

public class MeasureListActivity extends BaseActivity {
    @BindView(R.id.back_tv)
    HXTextView backTv;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.add_tv)
    HXTextView addTv;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.measure_vp)
    ViewPager measureVp;
    Unbinder unbinder;
    private List<String> mTitleDataList;
    private List<BaseFragment> fragments;
    public static final int MODULE = 1;
    public static final int STEEL = 2;
    public static final int CONCRETE = 3;
    public static final int MASONRY = 4;
    public static final int PLASTER = 5;
    private DeviceListPagerAdapter measureListPagerAdapter;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measure_list);
        unbinder = ButterKnife.bind(this);
        titleTv.setText("实测实量列表");
        if(hasAdd()){
            addTv.setVisibility(View.VISIBLE);
        }
        type = getIntent().getIntExtra("type", 1);
        initView();
    }

    private void initView(){
        measureVp.setOffscreenPageLimit(5);
        fragments = new ArrayList<BaseFragment>();
        fragments.add(MeasureFragment.newInstance(MODULE));
        fragments.add(MeasureFragment.newInstance(STEEL));
        fragments.add(MeasureFragment.newInstance(CONCRETE));
        fragments.add(MeasureFragment.newInstance(MASONRY));
        fragments.add(MeasureFragment.newInstance(PLASTER));
        measureListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        measureVp.setAdapter(measureListPagerAdapter);

        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("模板");
        mTitleDataList.add("钢筋");
        mTitleDataList.add("砼");
        mTitleDataList.add("砌筑");
        mTitleDataList.add("抹灰");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();//获取标题数量
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_adadad));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.main_color));//设置选择及未选择状态标题文字
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        measureVp.setCurrentItem(index);
                        type = index + 1;
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.main_color));//设置底部短横线
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(MeasureListActivity.this, 25);
            }
        });

//        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
//        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
//        fragmentContainerHelper.setDuration(300);
        measureVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
//                fragmentContainerHelper.handlePageSelected(position);
                type = position + 1;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
    }

    @OnClick({R.id.back_tv, R.id.add_tv})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back_tv:
                finish();
                break;
            case R.id.add_tv:
                intent = new Intent(MeasureListActivity.this, MeasureActivity.class);
                intent.putExtra("type", type);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && fragments != null) {
            for (BaseFragment baseFragment : fragments) {
                MeasureFragment measureFragment = (MeasureFragment) baseFragment;
                measureFragment.reLoad();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
